package com.species.web.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SpeciesUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.ImageClassifyVo;
import com.ruoyi.system.domain.vo.PrizeRedeemVo;
import com.ruoyi.system.domain.vo.ReportSpeciesVo;
import com.ruoyi.system.domain.vo.WxMaLoginForm;
import com.ruoyi.system.service.*;
import com.species.web.config.BaiduYunConfig;
import com.species.web.service.IUserService;
import nonapi.io.github.classgraph.json.Id;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author dries
 * @date 2022/3/19
 */
@RestController
@RequestMapping("/api/user")
public class UserController extends BaseController {

    @Autowired
    private ServerConfig serverConfig;
    @Resource
    private IUserService userService;
    @Resource
    private ISmsSpeciesService speciesService;
    @Resource
    private ISmsSpeciesReportService speciesReportService;
    @Resource
    private ISmsPrizeHistoryService prizeHistoryService;
    @Resource
    private IIntegrationChangeHistoryService integrationChangeHistoryService;

    /**
     * 登录方法
     *
     * @param vo 登录信息
     * @return 结果
     */
    @PostMapping("/wxLogin")
    public AjaxResult login(@RequestBody WxMaLoginForm vo) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        SpeciesUser user = userService.wxLogin(vo);
        ajax.put(Constants.USER, user);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @param openId 登录信息
     * @return 结果
     */
    @GetMapping("/info")
    public AjaxResult getUserInfo(@RequestParam String openId) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        SpeciesUser user = userService.getUserInfo(openId);
        ajax.put(Constants.USER, user);
        return ajax;
    }

    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody SpeciesUser speciesUser) {
        return toAjax(userService.updateSpeciesUser(speciesUser));
    }

    /**
     * 积分兑换
     *
     * @param prizeRedeemVo 积分兑换信息
     * @return 结果
     */
    @PostMapping("/prizeRedeem")
    public AjaxResult prizeRedeem(@Validated @RequestBody PrizeRedeemVo prizeRedeemVo) {

        return userService.prizeRedeem(prizeRedeemVo);
    }

    /**
     * 积分列表
     *
     * @param prizeHistory 查询参数
     * @return 结果
     */
    @GetMapping("/myPrizeList")
    public TableDataInfo myPrizeList(SmsPrizeHistory prizeHistory) {
        startPage();
        List<SmsPrizeHistory> list = prizeHistoryService.selectSmsPrizeHistoryList(prizeHistory);
        return getDataTable(list);
    }

    /**
     * 积分列表
     *
     * @param integrationChangeHistory 查询参数
     * @return 结果
     */
    @GetMapping("/integralList")
    public TableDataInfo integralList(IntegrationChangeHistory integrationChangeHistory) {
        startPage();
        List<IntegrationChangeHistory> list = integrationChangeHistoryService.selectIntegrationChangeHistoryList(integrationChangeHistory);
        return getDataTable(list);
    }

    /**
     * 物种上报列表
     *
     * @param speciesReport 查询参数
     * @return 结果
     */
    @GetMapping("/speciesReportList")
    public TableDataInfo speciesReportList(SmsSpeciesReport speciesReport) {
        startPage();
        List<SmsSpeciesReport> list = speciesReportService.selectSmsSpeciesReportList(speciesReport);
        return getDataTable(list);
    }

    @PostMapping(value = "/identification/upload", produces = "application/json;charset=UTF-8")
    public AjaxResult uploadFile(MultipartFile file, @RequestParam String type) throws IOException {

        AjaxResult success = AjaxResult.success();
        try {

            ImageClassifyVo imageClassifyVo;
            List<ImageClassifyVo> imageClassifyVoList = BaiduYunConfig.aipImageClassify(file, type);

            if (CollUtil.isNotEmpty(imageClassifyVoList)) {
                // 上传文件路径
                String filePath = RuoYiConfig.getUploadPath();
                // 上传并返回新文件名称
                String fileName = FileUploadUtils.upload(filePath, file);
                String url = serverConfig.getUrl() + fileName;
                success.put("fileName", fileName);
                success.put("url", url);

                imageClassifyVo = imageClassifyVoList.get(0);

                // 判断是否为外来物种
                List<SmsSpecies> species = speciesService.selectSmsSpeciesByName(imageClassifyVo.getName());
                if (CollUtil.isNotEmpty(species)) {
                    imageClassifyVo.setAlienSpecies(true);
                    success.put("species", species.get(0));
                }

            } else {
                imageClassifyVo = new ImageClassifyVo();
                imageClassifyVo.setName("暂无数据");
            }
            success.put("imageClassify", imageClassifyVo);

            return success;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 物种上报
     *
     * @param vo 物种上报
     * @return 结果
     */
    @PostMapping("/reportSpecies")
    public AjaxResult reportSpecies(@Validated @RequestBody ReportSpeciesVo vo) {

        return userService.reportSpecies(vo);
    }

}
